
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import QWebSettings
from JSInterface.SysControl import *
import numpy as np
import cv2
from Lib.CvCamera import *
from Lib.EQLabel import *
import cgitb 
import threading
import sys
import datetime
import time
import os


#窗体大小
windowWidth = 1024
windowHeight = 600

# 主窗体
class MainWindow(QMainWindow):
   
    def __init__(self):
        super().__init__()

        self.setWindowTitle("挥发性试验仪")
        self.resize(windowWidth,windowHeight)
        #使窗体总在最前端
        #self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

        #创建WebView容器
        self.browser = QWebView()
        #控制WebView容器所有连接本窗口打开
        self.browser.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.browser.page().linkClicked.connect(lambda url:self.browser.load(url))
        #添加浏览器到窗口中
        self.setCentralWidget(self.browser)
        self.browser.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled,True)
        # 解决Qt  ajax请求跨域问题
        self.browser.settings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)
        #开启摄像头预览
        #self.label = EQLabel(self)
        #self.label.mousePress.connect(lambda:(self.stopVideo()))
        #self.label.move(0,0)
        #self.label.resize(1024,600)
        ##QApplication.desktop().screenGeometry().width()
        #width =1024
        #height =600
        #self.cvCamera = CvCamera(self.label,width,height)
        #self.cvCamera.playVideoSignal.connect(self.playVideo)
        #指定打开界面的 URL
        self.browser.page().mainFrame().javaScriptWindowObjectCleared.connect(self.addObject)
        #self.browser.page().mainFrame().evaluateJavaScript()
        #print("file:///" + QFileInfo("UI/Page/NewUI/standby.html").absoluteFilePath())
        self.browser.load(QUrl("file:///" + QFileInfo("UI/Page/Login.html").absoluteFilePath()))
        #self.browser.load(QUrl("file:////home/pi/Downloads/11/UI/Page/Login.html"))
        #self.browser.load(QUrl("http://www.baidu.com"))


    #将Python对象注入到JavaScript
    def addObject(self):
        self.sysControl = SysControl()
        self.browser.page().mainFrame().addToJavaScriptWindowObject("sys",self.sysControl)

    #窗体关闭前执行
    def closeEvent(self,QCloseEvent):
        #os.system("poweroff")
        time.sleep(0.1)

    # 执行js函数
    def evalJs(self,js):
        self.browser.page().mainFrame().evaluateJavaScript(js)

  
if __name__ == "__main__":
    cgitb.enable(format ='text')
    def excepthook(type, value, trace):  
        '''''write the unhandle exception to log'''  
        print('Unhandled Error: %s: %s'%(str(type), str(value)))
        sys.__excepthook__(type, value, trace)  
    sys.excepthook = excepthook  
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " 启动窗体 ")
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    #mainWindow.showFullScreen()
    mainWindow.show()
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " 已启动 ")
    sys.exit(app.exec_())
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " 销毁窗体 ")
    del mainWindow
    del app

    